package HIR;

import javax.swing.*;

import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

public class SplashScreen extends JWindow {
	
	// static 
	  public void SplashScreenMain() {
		    // initialize the splash screen
		    splashScreenInit();
		    // do something here to simulate the program doing something that
		    // is time consuming
		    int delay = Integer.parseInt(this.Props.getProperty("splash.delay"));
		  
		    setProgress("CHECK Update" ,0);
		    try {
				Thread.sleep(500);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		    // HTTP request VErsion 
		    URL storm = null;
			try {
				storm = new URL(this.Props.getProperty("splash.url"));
	
	        URLConnection yc;

				yc = storm.openConnection();
				 BufferedReader in;
					in = new BufferedReader(
	                        new InputStreamReader(
	                        yc.getInputStream()));
					  String inputLine;

				        while ((inputLine = in.readLine()) != null) {}
				           // System.out.println(inputLine);
				        in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				 setProgress("Storm Site unvailable" ,100);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					 
					
				}
				
		
		
			//	e.printStackTrace();
			
	      
		    
		    
		    setProgress("OK" ,0);
		    splashScreenDestruct();
		//    System.exit(0);
		  }

		  private void splashScreenDestruct() {
		    setScreenVisible(false);
		  }

		  private void splashScreenInit() {
		    ImageIcon myImage = new ImageIcon(this.getClass().getResource("/images/SplashScreen.gif"))	;
		    //screen = new SplashScreen(myImage);
		    setLocationRelativeTo(null);
		    //setProgressMax(10);
		    setScreenVisible(true);
		  }
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	BorderLayout borderLayout1 = new BorderLayout();
	JLabel imageLabel = new JLabel();
	JPanel southPanel = new JPanel();
	FlowLayout southPanelFlowLayout = new FlowLayout();
	JProgressBar progressBar = new JProgressBar();
	ImageIcon imageIcon;
	private Properties Props;

	
	public SplashScreen(ImageIcon imageIcon) {
		this.imageIcon = imageIcon;
		try {
			jbInit();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public SplashScreen() {
		try {
		Props = new Properties();
		Props.load(this.getClass().getResourceAsStream("/HIR/interfaces.properties"));
		this.imageIcon =new ImageIcon(this.getClass().getResource(this.Props.getProperty("splash.splash1")))	;
		
	
			
			jbInit();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
		// TODO Auto-generated constructor stub
	}

	// note - this class created with JBuilder
	void jbInit() throws Exception {
		imageLabel.setIcon(imageIcon);
		this.getContentPane().setLayout(borderLayout1);
		southPanel.setLayout(southPanelFlowLayout);
		southPanel.setBackground(Color.BLACK);
		this.getContentPane().add(imageLabel, BorderLayout.CENTER);
		this.getContentPane().add(southPanel, BorderLayout.SOUTH);
		southPanel.add(progressBar, null);
		this.pack();
	}
	
	public void setProgressMax(int maxProgress)
	{
		progressBar.setMaximum(maxProgress);
	}
	
	public void setProgress(int progress)
	{
		final int theProgress = progress;
		SwingUtilities.invokeLater(new Runnable() {
								   public void run() {
								   progressBar.setValue(theProgress);
								   }
								   });
	}
	
	public void setProgress(String message, int progress)
	{
		final int theProgress = progress;
		final String theMessage = message;
		setProgress(progress);
		SwingUtilities.invokeLater(new Runnable() {
								   public void run() {
								   progressBar.setValue(theProgress);
								   setMessage(theMessage);
								   }
								   });
	}
	
	public void setScreenVisible(boolean b)
	{
		final boolean boo = b;
		SwingUtilities.invokeLater(new Runnable() {
								   public void run() {
								   setVisible(boo);
								   }
								   });
	}
	
	private void setMessage(String message)
	{
		if (message==null)
		{
			message = "";
			progressBar.setStringPainted(false);
		}
		else
		{
			progressBar.setStringPainted(true);
		}
		progressBar.setString(message);
	}

	public void initExec() {
	    splashScreenInit();
		// TODO Auto-generated method stub
		
	}
}